package com.jason.kotlin.gank.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.jason.kotlin.gank.model.GankCategoryBean
import com.jason.kotlin.gank.repository.GankApi
import com.jason.kotlin.gank.repository.GankApiImpl
import com.jason.library.common.base.BaseViewModel
import com.jason.library.common.base.DataWrapper
import kotlinx.coroutines.launch
import java.lang.Exception

/**
 * @description: GankCategoryViewModel
 *
 * @author ChenBaoyang
 * @date 4/19/21 15:13
 */
class GankCategoryViewModel(application: Application) : BaseViewModel(application) {

    val categoryArticle = MutableLiveData<DataWrapper<List<GankCategoryBean>>>()

    val categoryGanHuo = MutableLiveData<DataWrapper<List<GankCategoryBean>>>()

    private var gankApi: GankApi = GankApiImpl()

    fun loadGanHuoCategories() {
        viewModelScope.launch {
            showLoading()
            try {
                val response = gankApi.getCategoriesGanHuo()
                categoryGanHuo.postValue(DataWrapper.success(response.data))
                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun loadGArticleCategories() {
        viewModelScope.launch {
            showLoading()
            try {
                val response = gankApi.getCategoriesArticle()
                categoryArticle.postValue(DataWrapper.success(response.data))
                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }
}